class Reactive {
  public proxy: any;

  private getEffect(key: string | symbol, val: any) {
    console.log(`${key.toString()}属性值为：${val}`);
  }

  private setEffect(key: string | symbol, oldVal: any, newVal: any) {
    console.log(`属性${key.toString()}由${oldVal}设置为${newVal}`);
  }

  private deleteEffect(key: string | symbol) {
    console.log(`属性${key.toString()}被删除`);
  }

  constructor(obj) {
    if (typeof obj !== "object") throw Error("被代理的对象必须是object类型");
    const _this = this;
    this.proxy = new Proxy(obj, {
      set(target, key, value) {
        _this.setEffect(key, target[key], value);
        target[key] = value;
        return true;
      },
      get(target, key) {
        _this.getEffect(key, target[key]);
        return target[key];
      },
      deleteProperty(target, key) {
        _this.deleteEffect(key);
        delete target[key];
        return true;
      },
    });
  }
}

let obj = {
  a: 1,
  b: "jiji",
};

let reactive = new Reactive(obj);

reactive.proxy.a;
reactive.proxy.b = "ahahah";
delete reactive.proxy.b;

console.log(obj);
